/**
 * 
 */
package com.effectiv.gooruda.startup;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import com.effectiv.gooruda.domain.Lov;
import com.effectiv.gooruda.repository.LovRepository;

/**
 * @author Dhrubo
 * 
 */
@Component
@Slf4j
public class ApplicationStartupProcessor implements
		ApplicationListener<ContextRefreshedEvent> {

	
	private final String ROLES[] = { "ROLE_COMPANY_ADMIN", "ROLE_EMPLOYEE", "ROLE_NON_EMPLOYEE",
			"ROLE_SUPER_ADMIN" };
	
	
	private final String PRIORITY[] = { "HIGH", "MEDIUM", "LOW" };
	
	@Autowired
	private LovRepository lovRepository;

	public void onApplicationEvent(ContextRefreshedEvent event) {

		// create ---- super admin
		
		

		// check and load Lov data
		

		for (String s : ROLES) {
			Lov lov = lovRepository.findByName(s);
			
			if(lov == null){
				log.debug("Lov : {} not found adding to database.",s);
				
				lov = new Lov();
				lov.setDomain("ROLE");
				lov.setName(s);
				lov.setKey(s);
				
				lovRepository.save(lov);
			}
		}
		
		
		for (String s : PRIORITY) {
			Lov lov = lovRepository.findByName(s);
			
			if(lov == null){
				log.debug("Lov : {} not found adding to database.",s);
				
				lov = new Lov();
				lov.setDomain("PRIORITY");
				lov.setName(s);
				lov.setKey(s);
				
				lovRepository.save(lov);
			}
		}
	}

}
